$(function(){
	
	
	var zhe_checked = true;
	var no_hui = true;
	var jisuan = false;
	var shifu = $('.shifu').val();
	var checked = $('#checked').val();
	var no_hui_price,old_price,now_price,charge;
	var beishu = parseFloat($('.zhe').html());
	var unchecked = $('#unchecked').val();
	if(parseFloat($('.price_input').val())){
		$('.bucanyu').attr('max',parseFloat($('.price_input').val()));
		$('.bucanyu').removeAttr('readonly');
	}else{
		$('.bucanyu').attr('readonly','readonly');
	}
	console.log('init:'+$('.bucanyu').attr('readonly'));
	/*if(shifu === ''){
		$('.money_number').html('0');
	}
	$('.shifu').keyup(function(){
		shifu = $('.shifu').val();
		if(shifu == ''){
			$('.money_number').html('0');
		}else{
			$('.money_number').html(shifu);
		}
	})*/
	
	$('.check').click(function(){
		if(parseFloat($('.price_input').val())){
			$('.bucanyu').attr('max',parseFloat($('.price_input').val()));
			$('.bucanyu').removeAttr('readonly');
		}else{
			$('.bucanyu').attr('readonly','readonly');
		}
		console.log('check:'+$('.bucanyu').attr('readonly'));
		if(no_hui){
			jisuan = true;
			no_hui = false;
			$(this).css('background-image',checked);
			$('.no_charge_price').css('display','block');
			$('.bucanyu').val('');
		}else{
			jisuan = false;
			$(this).css('background-image',unchecked);
			$('.no_charge_price').css('display','none');
			no_hui = true;
			old_price = $('.price_input').val();
			charge = old_price*(1-beishu*0.1);
			$('.bucanyu').val('');
			$('.zhe_check').addClass('checked');
			$('.hui_price').addClass('hui_show');
			$('.price').html(charge.toFixed(2));
			$('.money_number').html((old_price-charge).toFixed(2));
			$('.money_text').val($('.money_number').html());
			$('.paySubmit span').html((old_price-charge).toFixed(2)+"  ");
			$('.paySubmit').css('background-color','#ff6634');
		}
	})
	
	$('.zhe_check').click(function(){
		if(parseFloat($('.price_input').val())){
			$('.bucanyu').attr('max',parseFloat($('.price_input').val()));
			$('.bucanyu').removeAttr('readonly');
		}else{
			$('.bucanyu').attr('readonly','readonly');
		}
		console.log('zhe_check:'+$('.bucanyu').attr('readonly'));
		if(zhe_checked){
			$(this).css('background-image',checked);
			zhe_checked = false;
		}else{
			$(this).css('background-image',unchecked);
			zhe_checked = true;
		}
	})
	$('.price_input').keyup(function(){
		if(parseFloat($('.price_input').val())){
			$('.bucanyu').attr('max',parseFloat($('.price_input').val()));
			$('.bucanyu').removeAttr('readonly');
		}else{
			$('.bucanyu').attr('readonly','readonly');
		}
		console.log('price_input:'+$('.bucanyu').attr('readonly'));
		no_hui_price = $('.bucanyu').val();
		if(no_hui_price == ''){
			no_hui_price = 0;
		}else{
			no_hui_price =parseFloat($('.bucanyu').val());
		}
		old_price =parseFloat($('.price_input').val());
		if(jisuan){
			if(old_price >= no_hui_price){
				//实际消费大于不参与优惠金额
				charge = (old_price - no_hui_price)*(1-beishu*0.1);
				$('.cannot').removeClass('show');
				$('.zhe_check').css('background-image',checked);
				$('.hui_price').css('display','block');
			}else{
				$('.cannot').addClass('show');
				$('.zhe_check').css('background-image','none');
				$('.hui_price').css('display','none');
				charge = 0;
			}
		}else{
			old_price = $(this).val();
			charge = old_price*(1-beishu*0.1);
		}
		$('.zhe_check').css('background-image',checked);
		$('.hui_price').addClass('hui_show');
		$('.price').html(charge.toFixed(2));
		$('.money_number').html((old_price-charge).toFixed(2));
		$('.money_text').val($('.money_number').html());
		$('.paySubmit span').html((old_price-charge).toFixed(2)+"  ");
		$('.paySubmit').css('background-color','#FE5722');		
	})
	$('.bucanyu').keyup(function(){
		if(parseFloat($('.price_input').val())){
			$('.bucanyu').attr('max',parseFloat($('.price_input').val()));
			$('.bucanyu').removeAttr('readonly');
		}else{
			$('.bucanyu').attr('readonly','readonly');
		}
		console.log('bucanyu:'+$('.bucanyu').attr('readonly'));
		no_hui_price = $('.bucanyu').val();
		if(no_hui_price == ''){
			no_hui_price = 0;
		}else{
			no_hui_price =parseFloat($('.bucanyu').val());
		}
		old_price =parseFloat($('.price_input').val());
			if(old_price >= no_hui_price){
				//实际消费大于不参与优惠金额
				charge = (old_price - no_hui_price)*(1-beishu*0.1);
				$('.cannot').removeClass('show');
				$('.zhe_check').css('background-image',checked);
				$('.hui_price').css('display','block');
			}else{
				$('.cannot').addClass('show');
				$('.zhe_check').css('background-image','none');
				$('.hui_price').css('display','none');
				charge = 0;
			}
		$('.zhe_check').addClass('checked');
		$('.hui_price').addClass('hui_show');
		if((old_price-charge).toFixed(2) === "NaN"){
			$('.paySubmit span').html('');
		}else{
			$('.paySubmit span').html((old_price-charge).toFixed(2)+"  ");
			$('.paySubmit').css('background-color','#ff6634');
			$('.money_text').val($('.money_number').html());
			$('.money_number').html((old_price-charge).toFixed(2));
			$('.price').html(charge.toFixed(2));
		}
	})	
	/*
	 * 确认买单
	 * */
	$('.paySubmit').on('touchstart',function(){
		$('.zhekou').submit();
	})
})
	
